package com.tanhua.dubbo.service;

import com.tanhua.model.domain.Friend;
import com.tanhua.model.mongoDb.Movement;
import com.tanhua.model.mongoDb.MovementTimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-03-01 0:27
 * @Desc 动态Service
 */
@Component
public class MovementsService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Async
    public void saveMovementsTimeLine(Movement movement) {
        //2.根据每一个好友Id创建userId-好友Id时间线表，并储存
        Criteria criteria = Criteria.where("userId").is(movement.getUserId());
        Query query = Query.query(criteria);
        List<Friend> ts = mongoTemplate.find(query, Friend.class);
        ts.forEach((item) -> {
            MovementTimeLine movementTimeLine = new MovementTimeLine();
            movementTimeLine.setMovementId(movement.getId());
            movementTimeLine.setCreated(System.currentTimeMillis());
            movementTimeLine.setFriendId(item.getFriendId());
            movementTimeLine.setUserId(item.getUserId());
            mongoTemplate.save(movementTimeLine);
        });
    }
}
